//
//  BChargeCashSubChargeController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/7.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BChargeCashSubChargeController.h"

//contentView
#import "BChargeCashChargeContentView.h"
#import "BCSavePhotoManager.h"
//request
#import "UserWalletChargeRequest.h"
#import "BCUserWalletCurrRequest.h"


//选择币种
#import "BChooseCoinTypeController.h"

@interface BChargeCashSubChargeController () <BChooseCoinTypeControllerDelegate>

@property (nonatomic, strong) BChargeCashChargeContentView *chargeContentView;



/**
 *币种列表数据
 */
@property (nonatomic, strong) BCUserWalletCurrData *walletCurrData;


@end

@implementation BChargeCashSubChargeController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    [self setupSubviews];
    //
    [self requestChargeCurrId:nil init:true change:false];
    //默认请求一次
    [self requestCurrList];
}

- (void)setupSubviews {
    self.chargeContentView = [BChargeCashChargeContentView new];
    [self.view addSubview:self.chargeContentView];
    [self.chargeContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.selectCurrencyButton.mas_bottom).offset(20.0);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    self.chargeContentView.alpha = 0;
    //
    [self.chargeContentView.addressCopyButton addTarget:self action:@selector(addressButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    [self.chargeContentView.saveQrButton addTarget:self action:@selector(saveButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
}


#pragma mark - actions
- (void)addressButtonDidClickedAction {
    NSString *pasteText = self.chargeContentView.chargeData.address;
    if (pasteText) {
        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
        [pasteBoard setString:pasteText];
        if (self.view) {
            NSString *success = BCLocalizedKey(@"general_copy_success");
            [QMUITips showSucceed:success inView:self.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:success]];
        }
    }
}
- (void)saveButtonDidClickedAction {
    UIImage *qrImage = self.chargeContentView.qrImage;
    if (qrImage) {
        WEAK_SELF
        [[BCSavePhotoManager sharedSave] savePhotoToAlbum:qrImage complete:^(BOOL success, SavePhotoManagerError *error) {
            if (success) {
                if (wself.view) {
                    NSString *success = BCLocalizedKey(@"general_save_success");
                    [QMUITips showSucceed:success inView:self.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:success]];
                }
            }
            if (error) {
                if (wself.view) {
                    NSString *errMsg = BCLocalizedKey(@"general_save_failure");
                    [QMUITips showError:errMsg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:errMsg]];
                }
            }
        }];
    }
}

#pragma mark -
- (void)selectCurrencyButtonDidClickedAction {
    BChooseCoinTypeController *coinTypeVC = [BChooseCoinTypeController new];
    coinTypeVC.walletCurrData = self.walletCurrData;
    coinTypeVC.delegate = self;
    [self.containerController presentViewController:coinTypeVC animated:true completion:nil];
}


#pragma mark - BChooseCoinTypeControllerDelegate
- (void)chooseCoinTypeControllerDidRequestCurrDataSuccess:(BCUserWalletCurrData *)currData {
    self.walletCurrData = currData;
}
- (void)chooseCoinTypeControllerDidSelectCurrDataModel:(BCUserWalletCurrDataModel *)dataModel {
    self.selectCurrencyButton.currData = dataModel;
    self.selectCurrencyButton.coinType = dataModel.acronyms;
    NSString *currId = [NSString stringWithFormat:@"%@",@(dataModel.cur_id)];
    [self requestChargeCurrId:currId init:false change:true];
}

#pragma mark - request
- (void)requestChargeCurrId:(NSString *)currId init:(BOOL)init change:(BOOL)change {
    if (init) {
        [self.view bc_showLoading];
    }
    if (change) {
        [self.chargeContentView showContentScroll:false];
        [self.chargeContentView bc_showLoading];
    }
    UserWalletChargeRequest *request = [UserWalletChargeRequest new];
    request.cur_id = currId;
    WEAK_SELF
    [request didRequestCompleted:^{
        if (init) {
            [wself.view bc_hiddenLoading];
        }
        if (change) {
            [wself.chargeContentView bc_hiddenLoading];
        }
    }];
    [request didRequestSuccessed:^(id result, id page) {
        UserWalletChargeResponse *resp = result;
        [wself handleData:resp.data init:init change:change];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error&&wself.view) {
            NSString *errorMsg = error.domain;
            [QMUITips showError:errorMsg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:errorMsg]];
            if (init) {
                [wself.view bc_showWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) error:error needRefresh:true refreshTitle:nil target:self refreshAction:@selector(requestInitFailedAction)];
            }
            if (change) {
                [wself.chargeContentView bc_showError:error needRefresh:true
                                refreshTitle:nil target:self refreshAction:@selector(requestChargeContentScrollFialedAction)];
            }
        }
    }];
    [request requestServer];
}
- (void)requestInitFailedAction {
    [self.view bc_hiddenError];
    [self requestChargeCurrId:nil init:true change:false];
}
- (void)requestChargeContentScrollFialedAction {
    [self.chargeContentView bc_hiddenError];
    NSString *currId = [NSString stringWithFormat:@"%@",@(self.selectCurrencyButton.currData.cur_id)];
    [self requestChargeCurrId:currId init:false change:true];
}

- (void)handleData:(UserWalletChargeData *)chargeData init:(BOOL)init change:(BOOL)change {
    if (chargeData) {
        if (init) {
            self.selectCurrencyButton.alpha = 1;
            self.chargeContentView.alpha = 1;
        }
        if (change) {
            [self.chargeContentView showContentScroll:true];
        }
        //设置数据
        self.selectCurrencyButton.caption = chargeData.flow.select;
        self.selectCurrencyButton.coinType = chargeData.curr;
        //
        self.chargeContentView.chargeData = chargeData;
    }
}





//默认请求一次币种列表信息
- (void)requestCurrList {
    BCUserWalletCurrRequest *request = [BCUserWalletCurrRequest new];
    //
    WEAK_SELF
    [request didRequestSuccessed:^(id result, id page) {
        BCUserWalletCurrResponse *resp = result;
        wself.walletCurrData = resp.data;
    }];
    [request requestServer];
}


@end
